//////////////////////////////////////////////////////////////////////////
//
// GAM203
// Player
// Player.cpp
// 
// Grant Johnson
// Qantm College
//
//////////////////////////////////////////////////////////////////////////

#include "Player.h"
#include "DXMgr.h"
#include "Renderer.h"

Player::Player(char* a_pMeshFileName)
{
	m_pMeshFileName = new char[256];
	//Copy over the file name, this is required as member data so it can be used later in the save file
	sprintf_s(m_pMeshFileName, 256, "%s", a_pMeshFileName);
	
	//Create the dwarf mesh
	m_mesh.CreateObject(DXMgr::GetInstance().GetDevice(), m_pMeshFileName);

	//Calculate the bounding box
	DWORD* pVertices = NULL;
	m_mesh.GetMesh()->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);
	D3DXComputeBoundingBox((D3DXVECTOR3*)pVertices, m_mesh.GetMesh()->GetNumVertices(), D3DXGetFVFVertexSize(m_mesh.GetMesh()->GetFVF()), &m_minBounds, &m_maxBounds);
	m_mesh.GetMesh()->UnlockVertexBuffer();

	m_collideBox.CreateObject(DXMgr::GetInstance().GetDevice(), NULL);
}

Player::~Player()
{
}

void Player::Update(float a_dt)
{
	D3DXMATRIX transMat;
	D3DXMATRIX rotMat;
	D3DXMATRIX scaleMat;

	D3DXMatrixTranslation(&transMat, m_pos.x, m_pos.y, m_pos.z);
	D3DXMatrixRotationYawPitchRoll(&rotMat, GetYaw(), GetPitch(), GetRoll());
	D3DXMatrixScaling(&scaleMat, 1, 1, 1);

	//Update the world matrix of the dwarf
	m_mesh.SetWorldMatrix(scaleMat * rotMat * transMat);

	D3DXMatrixScaling(&scaleMat, m_maxBounds.x - m_minBounds.x, m_maxBounds.y - m_minBounds.y, m_maxBounds.z - m_minBounds.z);
	D3DXMatrixIdentity(&transMat);
	D3DXMatrixTranslation(&transMat, m_pos.x, m_pos.y+1.0f, m_pos.z);

	m_collideBox.SetWorldMatrix(scaleMat * transMat);

	/*if(CheckCollision(D3DXVECTOR3(1,1,1), D3DXVECTOR3(1,1,1))
	{
		//Fire emitter
		FireEmitter* pEmitter = new FireEmitter();
		pEmitter->Setup(pElem->GetPos());
		Renderer::GetInstance().AddEmitter((Emitter*)pEmitter);
		pElem->SetLit(true);

		//Sound Emitter
		SoundEmitter* pFlameSound = SoundMgr::GetInstance().CreateEmitter("media/Sound/Flame.wav", true);
		pFlameSound->SetPosition(pElem->GetPos().x, pElem->GetPos().y, pElem->GetPos().z);
		pFlameSound->Play();
	}*/
}
D3DXVECTOR3 Player::GetCameraAttachPos() const
{
	D3DXVECTOR4 outputVec = D3DXVECTOR4(0.0f,5.0f,10.0f,0.0f);
	//Rotate by yaw pitch and roll
	D3DXMATRIX tempMat;
	D3DXMatrixRotationYawPitchRoll(&tempMat, GetYaw(), GetPitch(), GetRoll());
	D3DXVec4Transform(&outputVec, &outputVec, &tempMat);

	outputVec.x += m_pos.x;
	outputVec.y += m_pos.y;
	outputVec.z += m_pos.z;

	//convert from matrix to vector
	return (D3DXVECTOR3)outputVec;
}
D3DXVECTOR3 Player::GetCameraTrackPos() const
{
	D3DXVECTOR3 trackPos = m_pos;
	//Stop camera looking at his feet
	trackPos.y += 1;
	return trackPos;
}